<?php

class sfHijreeDateWidget extends sfWidgetFormDate{
    
  protected function configure($options = array(), $attributes = array())
  {
    sfProjectConfiguration::getActive()->loadHelpers('I18N');	
	$this->addOption('with_text', false);
    parent::configure($options, $attributes);
      
    $start_year = date('Y') - 680;
    $end_year = date('Y') - 578;
    $years = range($start_year, $end_year);
    
    $this->addOption('years', array_combine($years, $years));

	if($this->getOption('with_text')){
		$months_arr = array(
			1=>'محرم', 2=>'صفر', 3=>'ربيع الأول', 4=>'ربيع الآخر', 5=>'جمادى الأولى', 6=>'جمادى الآخرة', 7=>'رجب', 8=>'شعبان', 9=>'رمضان', 10=>'شوال', 11=>'ذو القعدة', 12=>'ذو الحجة',
		);
	}else{
		$months_arr = range(1,12);
		$months_arr = array_combine($months_arr, $months_arr);
	}
	
    $this->addOption('months', $months_arr);
    $this->addOption('format', __('Day').' %day% '.__('Month').' %month% '.__('Year').' %year% ');
  }
    
    public function render($name, $value = null, $attributes = array(), $errors = array()){
        
        $start_year = date('Y') - 680;
        $end_year = date('Y') - 678;
        $years = range($start_year, $end_year);
        
        $options['years'] = array_combine($years, $years);        
        
        // convert value to an array
        $default = array('year' => null, 'month' => null, 'day' => null);
        
        if (is_array($value))
        {
            $value = array_merge($default, $value);
        }
        else
        {
            //$value = (string) $value == (string) (integer) $value ? (integer) $value : strtotime($value);      
            $value = explode('-', $value);
            if(count($value) != 3){
                $value = false;
            }else{
                $tmp_val = $value;
                $value = array();
                $value['year']  = (int)$tmp_val[0];
                $value['month'] = (int)$tmp_val[1];
                $value['day']   = (int)$tmp_val[2];
            }
            
            if (false === $value)
            {
                $value = $default;
            }
            elseif(is_array($value)){
                // do nothing...
            }else
            {
                $value = array('year' => date('Y', $value), 'month' => date('m', $value), 'day' => date('d', $value));      
            }
        }

        $date = array();
        $emptyValues = $this->getOption('empty_values');

        $date['%day%'] = $this->renderDayWidget($name.'[day]', $value['day'], array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['day']) + $this->getOption('days') : $this->getOption('days'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes));
        $date['%month%'] = $this->renderMonthWidget($name.'[month]', $value['month'], array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['month']) + $this->getOption('months') : $this->getOption('months'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes));
        $date['%year%'] = $this->renderYearWidget($name.'[year]', $value['year'], array('choices' => $this->getOption('can_be_empty') ? array('' => $emptyValues['year']) + $this->getOption('years') : $this->getOption('years'), 'id_format' => $this->getOption('id_format')), array_merge($this->attributes, $attributes));

        return strtr($this->getOption('format'), $date);        
    }
}